package CyclicBarrierTest;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * @author zxs
 * @create 2021-03-31 16:55
 */
public class StuThread implements Runnable {
    private long time;
    private CyclicBarrier cyclicBarrier;

    public StuThread(long time, CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
        this.time = time;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "完成了考试,用时：" + time);
        try {
            cyclicBarrier.await();
            System.out.println(Thread.currentThread().getName()+" 出教室时间: "+System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
